package com.pozu.new_retail.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pozu.new_retail.dao.mapper.CommentMapper;
import com.pozu.new_retail.dao.mapper.FavoriteRecordMapper;
import com.pozu.new_retail.dao.mapper.UgcMapper;
import com.pozu.new_retail.dao.mapper.UpvoteMapper;
import com.pozu.new_retail.dao.model.Ugc;
import com.pozu.new_retail.dao.model.Upvote;
import com.pozu.new_retail.dao.vo.BriefUgcVo;
import com.pozu.new_retail.dao.vo.UgcVo;
import com.pozu.new_retail.service.UgcService;

@Service("ugcService")
public class UgcServiceImpl implements UgcService {
	
	@Autowired
	private UgcMapper ugcMapper;
	@Autowired
	private CommentMapper commentMapper;
	@Autowired
	private FavoriteRecordMapper favoriteRecordMapper;
	@Autowired
	private UpvoteMapper upvoteMapper;

	@Override
	public List<BriefUgcVo> queryMyByMap(HashMap<String, Object> map) {
		
		long userId = Long.parseLong(map.get("userId").toString());
		
		List<BriefUgcVo> vos = new ArrayList<BriefUgcVo>();
		
		List<BriefUgcVo> list = ugcMapper.queryMyByMap(map);
		
		if (null != list && list.size() > 0) {
			for(BriefUgcVo vo:list) {
				handleUgc(vo,userId,Integer.parseInt(map.get("appId").toString()));
				vos.add(vo);
			}
		}
		
		return vos;
	}

	@Override
	public List<BriefUgcVo> queryByMap(HashMap<String, Object> map) {
		
		long userId = Long.parseLong(map.get("userId").toString());
		
		List<BriefUgcVo> vos = new ArrayList<BriefUgcVo>();
		
		List<BriefUgcVo> list = ugcMapper.queryByMap(map);
		
		if (null != list && list.size() > 0) {
			for(BriefUgcVo vo:list) {
				handleUgc(vo,userId,Integer.parseInt(map.get("appId").toString()));
				vos.add(vo);
			}
		}
		
		return vos;
	}

	@Override
	public List<BriefUgcVo> queryByRecMap(HashMap<String, Object> map) {
		
		long userId = Long.parseLong(map.get("userId").toString());
		
		List<BriefUgcVo> vos = new ArrayList<BriefUgcVo>();
		
		List<BriefUgcVo> list = ugcMapper.queryByRecMap(map);
		
		if (null != list && list.size() > 0) {
			for(BriefUgcVo vo:list) {
				handleUgc(vo,userId,Integer.parseInt(map.get("appId").toString()));
				vos.add(vo);
			}
		}
		
		return vos;
	}

	@Override
	public UgcVo queryByUgcId(long ugcId,long userId) {
		
		UgcVo vo = ugcMapper.queryByUgcId(ugcId);
		
		if (null == vo) {
			return null;
		}

		HashMap<String, Object> hashMap = new HashMap<String,Object>();

		hashMap.put("otherId", vo.getUgcId());
		hashMap.put("type", 0);
		hashMap.put("appId", vo.getAppId());

		/** 点赞数量 **/
		Integer upvoteNumber = upvoteMapper.queryCountByMap(hashMap);
		
		hashMap.put("userId", userId);
		/** 是否点赞 **/
		Upvote upvote = upvoteMapper.queryByMap(hashMap);
		
		if (null == upvote || upvote.getState() == 1) {
			vo.setUpvoteFlag(false);	
		}else {
			vo.setUpvoteFlag(true);
		}

	    /** 评论数 **/
	    Integer commentNumber = commentMapper.queryCountByMap(hashMap);
		
	    hashMap.put("type", 1);
	    
		/** 收藏数量 **/
		Integer favoriteNumber = favoriteRecordMapper.queryCountByMap(hashMap);
		
		/** 是否收藏 **/
		Boolean favorite = favoriteRecordMapper.queryByMap(hashMap) == null ? false : true;
		
		vo.setUpvoteNumber(upvoteNumber);
		vo.setCommentNumber(commentNumber);
		vo.setFavoriteNumber(favoriteNumber);
		vo.setFavorite(favorite);
		
		return vo;
	}

	@Override
	public int saveUgc(Ugc ugc) {
		
		long time = System.currentTimeMillis();
		
		ugc.setCreatedTime(time);
		ugc.setUpdatedTime(time);
		
		return ugcMapper.insertUgc(ugc);
	}

	@Override
	public int updateUgc(Ugc ugc) {
		
		ugc.setUpdatedTime(System.currentTimeMillis());
		
		return ugcMapper.updateUgc(ugc);
	}
	
	public void handleUgc(BriefUgcVo vo,long userId,int appId) {
		
		HashMap<String, Object> hashMap = new HashMap<String,Object>();

		hashMap.put("appId", appId);
		hashMap.put("otherId", vo.getUgcId());
		hashMap.put("type", 0);

		/** 点赞数量 **/
		Integer upvoteNumber = upvoteMapper.queryCountByMap(hashMap);
		
		hashMap.put("userId", userId);
		/** 是否点赞 **/
		Upvote upvote = upvoteMapper.queryByMap(hashMap);
		
		if (null == upvote || upvote.getState() == 1) {
			vo.setUpvoteFlag(false);	
		}else {
			vo.setUpvoteFlag(true);
		}
		
		vo.setUpvoteNumber(upvoteNumber);
	
	}

	@Override
	public Ugc queryInfoByUgcId(long ugcId) {
		return ugcMapper.queryByUgcId(ugcId);
	}

	@Override
	public int queryMyCountByUserId(HashMap<String, Object> map) {
		return ugcMapper.queryMyCountByUserId(map);
	}
}
